const fs = require('fs');
const http = require('http')
const https = require('https')

const superagent = require('superagent');
const charset = require('superagent-charset');
// const baseUrl = 'http://www.nipic.com/index.html'; //输入任何网址都可以
const cheerio = require('cheerio');


const config = require('./config')
const pickMetaData = config.pickMetaData[3]
const delDir = require('../utils/delDir');

charset(superagent); // 编码，防止爬虫抓取的内容乱码
superagent.get(pickMetaData.url)
            .charset(config.encode)
            .end(function(err, res) {
                var items = [];
                if (err) {
                    console.log('ERR: ' + err);
                    res.json({ code: 400, msg: err, sets: items });
                    return;
                }
                var $ = cheerio.load(res.text); // 加载页面内容

                var imgEle = $(pickMetaData.selector)
                // var imgEle = $('.tb-thumb img'); // 指定获取内容
                // console.log('sres.text', sres.text)
                // console.log('imgEle', imgEle)

                imgEle.each(function (index, item) {
                    // var $item = $(item);
                    // var src = $item.attr('src');
                    console.log('***图片地址*****', $(this).prop(pickMetaData.imgAttr))
                    items.push($(this).prop(pickMetaData.imgAttr))
                })

                // res.json({ code: 200, msg: "", data: items });

                saveFile(items);
            });



//保存资源
function saveFile(imgUrlArr) {
    delDir('upload');
    console.log('该文件目录已删除!');
    fs.mkdir(config.downloadImgPath, function (err) {
        console.log('-----------------------')
        console.log('创建' + config.downloadImgPath + '文件夹成功');
        console.log('-----------------------')
        // fs.exists(config.downloadImgPath, function (res) {
            
        // })
        console.log('imgUrlArr', imgUrlArr)

        imgUrlArr.forEach(function (url, index) {
            saveImg(url, `${config.downloadImgPath}【${index}】.jpg`)
        })
    })
}

function saveImg(url, path) {
console.log('**********图片下载地址***********')
console.log(pickMetaData.imgAttrPrefixExt + url)
console.log('*********************')

// request
// .get(pickMetaData.imgAttrPrefixExt + url)
// // .send({ name: 'Manny', species: 'cat' })
// .set({
//     // 'API-Key': 'foobar',
//     // ' Accept': 'application/json' 
//     encode: 'binary'
// })
// .then(res => {
//    alert('yay got ' + JSON.stringify(res.body));
// });
    var downloadFileCb = (req, res) =>{
        var imgData = '';
        req.setEncoding('binary');
        req.on('data', function (chunk) {
            imgData += chunk;
        })
        req.on('end', function () {
            fs.writeFile(path, imgData, 'binary', function (err) {
                console.log('保存图片成功' + path)
            })
        })
    }

    if((pickMetaData.imgAttrPrefixExt + url).indexOf('https') > -1){
        console.log('*****使用https*****')
        https.get(pickMetaData.imgAttrPrefixExt + url, downloadFileCb);
    }else if((pickMetaData.imgAttrPrefixExt + url).indexOf('http') > -1){
        console.log('*****使用http*****')
        http.get(pickMetaData.imgAttrPrefixExt + url, downloadFileCb);
    }
}